﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nhom14_HTTTHD_BuyMe.function
{
    public class Website
    {
        //Insert new Website
        static public int AddWebsite(int id_account, int SiteStatus, string SiteName, string SiteURL, string Description, string Visitor, string PageView, string Income, string Cost, string Disk, String Bandwidth, string member, string price, int id_category)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    website obj = new website();
                    obj.id_account = id_account;
                    obj.id_website_status = SiteStatus;
                    obj.id_type = 2;
                    obj.timestamp = DateTime.Now;
                    obj.site_name = SiteName;
                    obj.site_url = SiteURL;
                    obj.description = Description;
                    obj.visitiors = Visitor;
                    obj.page_views = PageView;
                    obj.income = Income;
                    obj.cost_to_run = Cost;
                    obj.disk_space_used = Disk;
                    obj.bandwidth_used = Bandwidth;
                    obj.members = member;
                    obj.asking_price = price;
                    obj.date_started = DateTime.Now;
                    obj.stats_availabale = "0";
                    obj.status = 1;

                    category obj_c = new category();
                    var result = from x in context.categories
                                 where x.id == id_category
                                 select x;
                    obj_c = result.First();
                    obj.categories.Add(obj_c);

                    context.websites.AddObject(obj);
                    context.SaveChanges();

                    obj.image = "Images/" + obj.id + ".jpg";
                    context.SaveChanges();
                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        //Get the user's ID the Website belongs to
        static public int GetAccountID(int id)
        {
            try
            {
                websitebrokenEntities context = new websitebrokenEntities();

                var result = from x in context.websites
                             where x.id == id
                             select x;

                if (result.Count() == 0)
                    return 0;

                website web = result.First();

                return (int)web.id_account;
            }
            catch
            {
                return 0;
            }
        }

        //Update Post's type (Free or Premiere)
        static public int UpdateType(int id, int type)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.websites
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    website web = kq.First();

                    web = kq.First();
                    web.id_type = type;

                    context.SaveChanges();
                    return web.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int CloseWebsite(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.websites
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    website web = kq.First();

                    web = kq.First();
                    web.status = 0;

                    context.SaveChanges();
                    return web.id;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}